package com.solming.leetcode.question;

public class LengthOfLastWord58 {


    public static int lengthOfLastWord(String s) {
        char[] chars = s.toCharArray();
        int len = s.length()-1;
        int start = len;
        int end = len;
        boolean flag = true;
        for (int i=len;i>=0;i--){
            if (chars[i] != ' '){
                end = i-1;
                if (flag) {
                    start = i;
                    flag = false;
                }
            }

            if (!flag && chars[i] == ' '){
                return start - end;
            }
        }
        return start - end;

    }

    public static int lengthOfLastWord1(String s) {
       String[] arr = s.split(" ");
        for (int i =arr.length-1;i>0;i--){
            if (!" ".equals(arr[i] )){
                return arr[i].length();
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfLastWord(" sssss  ss "));
    }


}
